//1、导入外部的模块
let http = require('http')
let fs = require('fs')
let url = require('url')
let path = require('path')
let mime = require('./js/mime.js')
let login = require('./js/login.js')
//2、创建服务
let server = http.createServer(function(request, response) {
  //4、设置跨域
  response.setHeader('Access-Control-Allow-Origin', '*')
  //5、解析request对象
  let requestUrl = request.url
  //6、使用url模块对requestUrl解析,加上true参数，query是对象
  let URL = url.parse(requestUrl, true)
  //7、获得请求的路径
  let pathName = URL.pathname
  //8、获得请求参数，使用对象方式
  let username = URL.query.username
  let password = URL.query.password
  console.log(pathName, username, password)
  //8、根据路径判断要执行的操作--->路由
  if (pathName === '/login') {
    //执行登录操作
      response.setHeader('Content-type', 'text/html;charset=UTF-8')
      let object = {}
      if (username === 'admin' && password === '123456') {
        object.meta = { status: 200, msg: '登录成功' }
        object.data = login.person
        response.end(JSON.stringify(object))
      }else{
        object.meta = { status: 201, msg: '错误的用户名或密码' }
        object.data = null
        response.end(JSON.stringify(object))
      }
  } else {
    response.end('else')
  }
})

//3、启动服务，监听请求
server.listen(8011, '127.0.0.1', () => {
  console.log(`Server is running on http://127.0.0.1:8011 !`)
})

